﻿using ProjectCore.Application.AppUser.Dto;
using ProjectCore.Domain.DomainService;
using ProjectCore.Domain.Repository.Interfaces;
using ProjectCore.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
using ProjectCore.Common;

namespace ProjectCore.Application
{
    public class AppUsers : IAppUsers
    {
        private readonly IBaseRepository<UserI> baseRepository;
        private readonly MyContext myContext;
        private readonly IUserDomainService userGroup;
        public AppUsers(IBaseRepository<UserI> _baseRepository, MyContext _myContext, IUserDomainService _userGroup)
        {
            baseRepository = _baseRepository;
            myContext = _myContext;
            userGroup = _userGroup;
        }

        //显示
        public async Task<HeaderResult<List<UserI>>> GetUserIs(int pageIndex, int pageSize)
        {
            try
            {
                var l = from a in myContext.Users
                        join b in myContext.PicUrls
                        on a.Photo equals b.ID into us
                        from uu in us.DefaultIfEmpty()
                        select new UserI
                        {
                            UserID = a.UserID,
                            Photo = uu.ID == 0 ? "" : uu.Photo,
                            UserName = a.UserName,
                            Sex = a.Sex,
                            Contry = a.Contry,
                            Prov = a.Prov,
                            City = a.City,
                            CreateTime = a.CreateTime
                        };
                int count = l.ToList().Count();
                l = l.Skip((pageIndex - 1) * pageSize).Take(pageSize);


                HeaderResult<List<UserI>> result = new HeaderResult<List<UserI>>()
                {
                    IsSucceed = true,
                    Result = await l.ToListAsync(),
                    Total = count,
                };
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }


        //删除
        public async Task<int> DelUserI(int Id)
        {
            return await userGroup.DelUserI(Id);
        }
    }
}
